package com.helloandroid.services.deamon

import android.app.Service
import android.content.Intent
import android.os.IBinder
import com.helloandroid.services.ServerService
import com.helloandroid.tools.MyLog
import kotlinx.coroutines.*
import java.io.File
import kotlin.concurrent.thread

class DeamonService: Service() {

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onCreate() {
        super.onCreate()
        MyLog.info(">>>>>>>>>>启动 DeamonService <<<<<<<<<<")
        startDeamonProcess()
        startEventLoop()

//        val intent = Intent(this,ServerService::class.java)
//        intent.putExtra("deamon",1)
//        startService(intent)

//        val file1 = File(filesDir.absolutePath + "/p1.txt")
//        MyLog.info("file1 full path:${file1.absolutePath}")
//        if (!file1.exists()){
//            val ret = file1.createNewFile()
//            MyLog.info("创建文件结果为:${ret}")
//        }else{
//            MyLog.info("file1已存在!")
//        }
        thread {
            startGuerProcess()
        }
    }

    var deamonCounter = 0
    fun startEventLoop(){
        CoroutineScope(Dispatchers.Default).launch{
            while (isActive){
                delay(2000)
                deamonCounter++
                MyLog.info("deamonCounter:${deamonCounter}")
            }
        }
    }

    external fun startDeamonProcess():Unit

    external fun startGuerProcess():Unit

    companion object{
        init {
            System.loadLibrary("native-lib")
        }
    }

}